package com.seeyon.apps.cxgd.event;

import com.alibaba.fastjson.JSON;
import com.seeyon.apps.cxgd.kit.HttpKit;
import com.seeyon.apps.cxgd.kit.LogKit;
import com.seeyon.apps.cxgd.kit.SameBusinessTripKit;
import com.seeyon.apps.cxgd.kit.StrKit;
import com.seeyon.ctp.common.formula.FormulaUtil;
import com.seeyon.ctp.common.log.CtpLogFactory;
import com.seeyon.ctp.organization.bo.V3xOrgDepartment;
import com.seeyon.ctp.organization.event.AddDepartmentEvent;
import com.seeyon.ctp.organization.event.UpdateDepartmentEvent;
import com.seeyon.ctp.util.annotation.ListenEvent;
import org.apache.commons.logging.Log;

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;


public class DeptAddOrUpdateListenerEvent {

    private static final Log log = CtpLogFactory.getLog(DeptAddOrUpdateListenerEvent.class);

    @ListenEvent(event = AddDepartmentEvent.class, async = true)
    public void addDept(AddDepartmentEvent event) {
        try {
            log.info("进入添加部门监听-------------------------------------");
            String same_business_trip_url = StrKit.str(FormulaUtil.getVar("same_business_trip_url"));
            String secret = StrKit.str(FormulaUtil.getVar("tcSecret"));
            V3xOrgDepartment v3xOrgDepartment = event.getDept();
            String accessToken = SameBusinessTripKit.getSameBusinessTripToken();
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("accessToken", accessToken);
            map.put("isEncrypt", false);
            map.put("random", UUID.randomUUID().toString());
            map.put("timestamp", System.currentTimeMillis());
            Map<String, Object> param_map = new HashMap<String, Object>();
            param_map.put("outdepartId", StrKit.str(v3xOrgDepartment.getId()));
            String code = StrKit.str(v3xOrgDepartment.getCode());
            String name = StrKit.str(v3xOrgDepartment.getName());
            param_map.put("departName", name);
            Long superior = v3xOrgDepartment.getSuperior();
            if (superior == null) {
                param_map.put("outParentId", "");
            } else {
                String path = v3xOrgDepartment.getPath();
                if (path.length() == 12) {
                    param_map.put("outParentId", "");
                } else {
                    param_map.put("outParentId", StrKit.str(superior));
                }
            }
            boolean enabled = v3xOrgDepartment.getEnabled();
            if (enabled) {
                param_map.put("disabled", 0);
            } else {
                param_map.put("disabled", 1);
            }
            param_map.put("outEnterpriseId", "");
            map.put("param", param_map);
            String data_json = JSON.toJSONString(map);
            String dataMd5 = SameBusinessTripKit.stringToMD5(data_json);
            String signStr = dataMd5 + secret;
            String sign = SameBusinessTripKit.stringToMD5(signStr);
            String url = same_business_trip_url + "/openapi/api/Enterprise/DepartmentSync?sign=" + sign;
            String result = HttpKit.httpsPost(url, new HashMap<String, Object>(), data_json);
            log.info("同程部门新增请求结果:" + result);
            LogKit.addLog(name + "_" + code, "同程部门新增", data_json, result);
        } catch (Exception e) {
            log.error("同程部门新增失败:", e);
        }

    }

    @ListenEvent(event = UpdateDepartmentEvent.class, async = true)
    public void updateDept(UpdateDepartmentEvent event) {
        try {
            log.info("进入更新部门监听-------------------------------------");
            String same_business_trip_url = StrKit.str(FormulaUtil.getVar("same_business_trip_url"));
            String secret = StrKit.str(FormulaUtil.getVar("tcSecret"));
            V3xOrgDepartment v3xOrgDepartment = event.getDept();
            String accessToken = SameBusinessTripKit.getSameBusinessTripToken();
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("accessToken", accessToken);
            map.put("isEncrypt", false);
            map.put("random", UUID.randomUUID().toString());
            map.put("timestamp", System.currentTimeMillis());
            Map<String, Object> param_map = new HashMap<String, Object>();
            param_map.put("outdepartId", StrKit.str(v3xOrgDepartment.getId()));
            String code = StrKit.str(v3xOrgDepartment.getCode());
            String name = StrKit.str(v3xOrgDepartment.getName());
            param_map.put("departName", name);
            Long superior = v3xOrgDepartment.getSuperior();
            if (superior == null) {
                param_map.put("outParentId", "");
            } else {
                String path = v3xOrgDepartment.getPath();
                if (path.length() == 12) {
                    param_map.put("outParentId", "");
                } else {
                    param_map.put("outParentId", StrKit.str(superior));
                }
            }
            boolean enabled = v3xOrgDepartment.getEnabled();
            if (enabled) {
                param_map.put("disabled", 0);
            } else {
                param_map.put("disabled", 1);
            }
            param_map.put("outEnterpriseId", "");
            map.put("param", param_map);
            String data_json = JSON.toJSONString(map);
            String dataMd5 = SameBusinessTripKit.stringToMD5(data_json);
            String signStr = dataMd5 + secret;
            String sign = SameBusinessTripKit.stringToMD5(signStr);
            String url = same_business_trip_url + "/openapi/api/Enterprise/DepartmentSync?sign=" + sign;
            String result = HttpKit.httpsPost(url, new HashMap<String, Object>(), data_json);
            log.info("同程部门更新请求结果:" + result);
            LogKit.addLog(name + "_" + code, "同程部门更新", data_json, result);
        } catch (Exception e) {
            log.error("同程部门更新失败:", e);
        }
    }

}
